﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace project_phoenix.source
{
    public class EntityManager
    {
        // the list of players
        List<PlayableEntity> players = new List<PlayableEntity>();
        List<NonPlayableEntity> mNPEs = new List<NonPlayableEntity>();

        bool isInitialized;

        public EntityManager()
        {

            Initialize();
            //LoadContent();
        }

        public void Initialize()
        {
            isInitialized = true;
        }

        public void LoadContent(ContentManager content)
        {
            // load all of the players content
            foreach (PlayableEntity entity in players)
            {
                entity.LoadContent();
                entity.SetupDefaultAnimations();
            }
        }


        public void Draw(GameTime gameTime)
        {
            foreach (PlayableEntity entity in players)
            {
                entity.Draw();
            }
            foreach (NonPlayableEntity npe in mNPEs)
            {
                npe.Draw();
            }

        }

        public void Update(GameTime gameTime)
        {
            // if we are in the idle state, lets set the animation
            foreach (PlayableEntity entity in players)
            {
                entity.Update(gameTime);
            }
            foreach (NonPlayableEntity npe in mNPEs)
            {
                npe.Update(gameTime);
            }
        }

        public void AddPlayable(PlayableEntity player)
        {
            // set our players internal manager to be this instance
           // player.EntityManager = this;

            // If we have a graphics device, tell the player to load content.
            //if (isInitialized)
            player.LoadContent();
            players.Add(player);
            player.SetupDefaultAnimations();
        }

        public void SpawnPlayer(int playerIndex, Vector2 position)
        {
            // only spawn them if they aren't alive
            if (!players[playerIndex].Alive)
            {
                players[playerIndex].Spawn(position);
            }
        }

        public void SpawnNPE(string type, Vector2 pos)
        {
            NonPlayableEntity tmp;
            switch (type)
            {
                case "Lesser Hellspawn":
                    {
                        tmp = new LesserHellSpawn();
                        break;
                    } 
                default:
                    {
                        tmp = new Imp();
                        break;
                    };
            }
            tmp.LoadContent();
            tmp.SetupDefaultAnimations();
            mNPEs.Add(tmp);
            tmp.Spawn(pos);
        }

        // TODO : move the extra stuff into the player class

        public void MovePlayerLeft(int playerIndex)
        {
            players[playerIndex].MoveLeft();
        }
        public void StopMove(int playerIndex)
        {
            players[playerIndex].StopMove();
        }

        public void MovePlayerRight(int playerIndex)
        {
            players[playerIndex].MoveRight();
        }

        public void MovePlayerUp(int playerIndex)
        {
            players[playerIndex].MoveUp();
        }

        public void MovePlayerDown(int playerIndex)
        {
            players[playerIndex].MoveDown();
        }

        public void Attack(int playerIndex)
        {
            players[playerIndex].Attack();
        }

        public void RemovePlayable(PlayableEntity player)
        {
        }


        #region Properties
        public List<PlayableEntity> Players
        {
            get { return players; }
        }
        #endregion



    }
}
